/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * ContractItem.java
 * business
 * 2013-2-21下午3:42:05
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.entity;

import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Type;
import org.jackysoft.report.ProductValidation;
import org.jackysoft.report.TradeType;
import org.jackysoft.util.SecurityGrade;
import org.joda.time.DateTime;

import com.google.common.collect.Lists;

@Entity
public class ContractItem extends AbstractEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8697509248322094695L;
	protected Product owner;
	protected Double price = 0D;		
	protected ContractClient client;
	protected Contract contract;
	protected ProductValidation validation;	
	protected DateTime fireDate;
	protected ContractType contractType;
	protected TradeType tradeType;
	protected SecurityGrade security = SecurityGrade.秘密;
	protected Double storage;
	protected Double amount = 0D;
	protected String remark;
	protected Collection<ContractDataItem> dataItems = Lists.newArrayList();
	
	
	
	@ManyToOne
	public Product getOwner() {
		return owner;
	}
		
	@ManyToOne
	public ContractClient getClient() {
		return client;
	}
	
	@ManyToOne
	public Contract getContract() {
		return contract;
	}
	
	
	public ProductValidation getValidation() {
		return validation;
	}
	
	public Double getStorage() {
		return storage;
	}
	public Double getAmount() {
		return amount;
	}
	public String getRemark() {
		return remark;
	}
	
	@OneToMany(orphanRemoval=true)
	@Cascade({CascadeType.ALL,CascadeType.DELETE,CascadeType.SAVE_UPDATE})
	public Collection<ContractDataItem> getDataItems() {
		return dataItems;
	}
	public void setOwner(Product owner) {
		this.owner = owner;
	}
	public void setClient(ContractClient client) {
		this.client = client;
	}
	public void setContract(Contract contract) {
		this.contract = contract;
	}
	public void setValidation(ProductValidation validation) {
		this.validation = validation;
	}
	public void setStorage(Double storage) {
		this.storage = storage;
	}
	public void setAmount(Double amount) {
		this.amount = amount;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public void setDataItems(Collection<ContractDataItem> dataItems) {
		this.dataItems = dataItems;
	}
	
	
	@Type(type = "jodatime")
	public DateTime getFireDate() {
		return fireDate;
	}

	public ContractType getContractType() {
		return contractType;
	}

	public void setFireDate(DateTime fireDate) {
		this.fireDate = fireDate;
	}

	public void setContractType(ContractType contractType) {
		this.contractType = contractType;
	}


	@Override
	public boolean equals(Object obj) {
		if(this.id==null || obj==null) return false;
		
		ContractItem o = (ContractItem) obj;
	
		if(o==null || o.id==null) return false;
		
		
		return this.id.equalsIgnoreCase(o.id);
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	/**
	 * 合同单项金额 
	 * */
	@Transient
	public Double getItemAmount() {
		return this.getAmount()*this.getPrice();
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
	}

	public SecurityGrade getSecurity() {
		return security;
	}

	public void setSecurity(SecurityGrade security) {
		this.security = security;
	}

	public TradeType getTradeType() {
		return tradeType;
	}

	public void setTradeType(TradeType tradeType) {
		this.tradeType = tradeType;
	}	
	
	
}